<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>用户管理</title>
	<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/productService.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/commonService.js'></script>
	<script>
		var saveOrUpdateUrl = "";
		function savePro(){  
		    $('#fm').form('submit',{  
		        url:saveOrUpdateUrl,  
		        onSubmit: function(){  
		        	var password = $("#user\\.password");
		        	var rpassword = $("#rpassword");
		        	if(password.val() != rpassword.val()){
		        		$.messager.alert("提示信息","输入的两次密码不一致!");
		        		password.val('');
		        		rpassword.val('');
		        		return false;
		        	}
		            return $(this).form('validate');  
		        },  
		        success: function(result){  
		            var result = eval('('+result+')');
		            var msg = result.name;
		            if($("#store\\.id").val()!=""){
		            	msg = msg + "修改成功";
		            }else{
		            	msg = msg + "新增成功";
		            }
		            if (result!=''){
		            	$.messager.show({title:"提示信息:",msg:msg,showType:'slide'});
		                $('#dlg').dialog('close');      // close the dialog  
		                $('#products').datagrid('reload');    // reload the user data  
		            } else {  
		                $.messager.show({  
		                    title: '新增成功',  
		                    msg: result.msg  
		                });  
		            }  
		        }  
		    });  
		}

		function toEditPro(){
			saveOrUpdateUrl = "<%=request.getContextPath()%>/adminmanage/addOrUpdateUser.action";
			var node=$("#products").datagrid("getSelected");
			if(node == null){
				$.messager.alert("提示信息:","请选择用户!");
				return;
			}
			$('#dlg').dialog('open').dialog('setTitle','修改门店');
			$('#dlg').form('clear');
			$("#storeid").val(node.storeId);
			$("#storename").val(node.storeName);
			$("#user\\.id").val(node.id);
			$("#user\\.loginName").val(node.loginName);
			$("#user\\.name").val(node.name);
			$("#user\\.password").val(node.password);
			$("#rpassword").val(node.password);
			$("#user\\.phone").val(node.phone);
			$("#user\\.address").val(node.address);
			$("#user\\.note").val(node.note);
			var ads = $("input[name='user\\.isAdmin']");
			if(node.isAdmin == '1'){
				ads[1].checked = true;
			}else if(node.isAdmin == '2'){
				ads[2].checked = true;
			}else{
				ads[0].checked = true;
			}
		}
		
		$(function(){
			// 用户列表
			$('#products').datagrid({
				nowrap: false,
				striped: true,
				url:'<%=request.getContextPath()%>/adminmanage/getAllUser.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				title:'用户列表',
				columns:[[
					{field:'loginName',title:'登录名',width:120},
					{field:'name',title:'姓名',width:120},
					{field:'role',title:'角色',width:50,
						formatter:function(value,row,index){
							if(row.isAdmin=="1"){
								return "<font color='blue'>店长</font>";
							}
							else if(row.isAdmin=="2"){
								return "<font color='red'>管理员</font>";
							}else {
								return "员工";
							}
						}
					},
					{field:'storeName',title:'所属门店',width:120},
					{field:'phone',title:'联系电话',width:120},
					{field:'address',title:'地址',width:150,rowspan:2},
					{field:'createTime',title:'创建时间',width:120,rowspan:2,sortable:true},
					{field:'loginTime',title:'最后登录时间',width:120,rowspan:2,sortable:true},
					{field:'note',title:'备注',width:200,rowspan:2}
				]],
				pageSize:15,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
			});
			// 门店列表
			$('#stores').datagrid({
				iconCls:'icon-save',
				nowrap: false,
				striped: true,
				collapsible:true,
				url:'<%=request.getContextPath()%>/adminmanage/getALlStore.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				columns:[[
					{field:'companyName',title:'商家名称',width:120},
					{field:'bossName',title:'店主姓名',width:120}
				]],
				pageSize:10,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
		});
			
			jQuery('a[name=query]').click(function(){
				var proName = $('#proName').val();
				$('#products').datagrid('reload',{name:proName});
			});
			jQuery('a[name=queryWare]').click(function(){
				var proName = $('#wareName').val();
				$('#stores').datagrid('reload',{name:proName});
			});
			$('#proName').keydown(function(e){
			      if(e.keyCode==13) {
			    	  var proName = $('#proName').val();
			    	  $('#products').datagrid('reload',{name:proName});
			      }
			    });
		}); 
		function toAddPro(){
			saveOrUpdateUrl = "<%=request.getContextPath()%>/adminmanage/addOrUpdateUser.action";
			$('#dlg').dialog('open').dialog('setTitle','新增用户');
			$('#dlg').form('clear');
		}
		function toDelUser(){
			var node = $('#products').datagrid('getSelected');
			var condt = new delOrgProWhCondtn();
   			condt.uid = node.id;
			commonService.valDelOrgProWH(condt,function(data){
				if(data=="no"){
					$.messager.alert("提示信息","该用户已在业务单据中使用，不能删除!",'warning');
					return;
	        	}
				$.messager.confirm('删除用户信息', '确认删除[' + node.name + ']吗?', function(r){
					if(r){
						$.ajax({
							type:"post",
							data:"id=" + node.id,
							url:"<%=request.getContextPath()%>/adminmanage/delUser.action",
							success:function(result){
								var data = eval('(' + result + ')');
								$.messager.show({title:"提示信息",msg: node.name + "删除成功",showType:'slide'});
								$('#products').datagrid('reload');
							}
						});
						
					}
				});
			});
		}

		function openSelectStore(){
	   		var title = "门店选择";
	   		$('#storeDlg').dialog('open').dialog('setTitle',title);  
   		}	
   		function selectStore(){
	   		var node = $('#stores').datagrid("getSelected");
	   		$("#storename").val(node.companyName);
	   		$("#storeid").val(node.id);
	   		$('#storeDlg').dialog('close');
   		}
		function clearData(){
			$('#proName').val('');
		}
		
	</script>
</head> 
<body class="easyui-layout" >
	<!-- 
	<h2>商品列表</h2>
	<div class="demo-info" style="margin-bottom:10px">
		<div class="demo-tip icon-tip"></div>
		<div>Click the row to start editing.</div>
	</div>
	 -->
	<div region="center" title="用户信息管理" style="overflow:hidden;">
	<div id="tb" class="datagrid-toolbar">  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:toAddPro()">新增</a>  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:toEditPro();">修改</a>  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:toDelUser()">删除</a>
	</div>
	
	<table style="font-size:12px; background:#fafafa; padding:10px;" border="0">
		<tr>
			<td>登录帐号
				<input type="text" id="proName" name="proName" class="easyui-validatebox"  value="${proName}" />
				<!-- 
				<input type="hidden" id="pcId" name="pcId" value="" />
				 -->
			</td>
			<td>
				<a href="#" class="easyui-linkbutton" name="query">查询</a>
 				<a href="#" class="easyui-linkbutton" onclick="clearData()">重置</a>
			</td>
		</tr>
	</table>

	<table id="products"></table>
	</div>
	<div id="dlg" class="easyui-dialog" style="width:400px"  
        closed="true" buttons="#dlg-buttons" resizable="true">  
    <form id="fm" method="post">
    <center>
    <table border="0">
    	<input id="user.id" name="user.id" type="hidden" />  
        	<tr>
        		<td>登录帐号：</td>
        		 <td>
        		 	<input id="user.loginName" name="user.loginName" class="easyui-validatebox" required="true" size="20" maxlength="50" />
        		 </td>  
        	</tr>
        	<tr>
        		<td>密码：</td>
            	<td>
            		<input id="user.password" name="user.password" class="easyui-validatebox" maxlength="20" required="true" type="password" onclick="this.value=''"/>
            	</td>
            </tr>
            <tr>
        		<td>重复密码：</td>
            	<td>
            		<input id="rpassword" name="rpassword" class="easyui-validatebox" maxlength="20" required="true" type="password" onclick="this.value=''"/>
            	</td>
            </tr>
            <tr>
        		<td>所属门店：</td>
            	<td>
            		<input id="storename" name="storename" class="easyui-validatebox" maxlength="20" required="true" readonly="readonly" onclick="openSelectStore()"/>
            		<input type="hidden" id="storeid" name="storeid" />
            	</td>
            </tr>
            <tr>
        		<td>角色：</td>
        		<td>
            		<input id="user.isAdmin" name="user.isAdmin" type="radio" value="0" />员工<input id="user.isAdmin" name="user.isAdmin" type="radio" value="1" />店长&nbsp;<input id="user.isAdmin" name="user.isAdmin" type="radio" value="2" checked/>管理员
            	</td>
            </tr>
            <tr>
        		<td>姓名：</td>
            	<td>
            		<input id="user.name" name="user.name" class="easyui-validatebox" maxlength="20" required="true" />
            	</td>
            </tr>
            <tr>
        		<td>联系电话：</td>
            	<td>
            		<input id="user.phone" name="user.phone" required="true"/>
            	</td>
            </tr>
            <tr>
        		<td>住址：</td>
            	<td>
            		<input id="user.address" name="user.address"  />
            	</td>
            </tr>
            <tr>
            	<td>描述：</td>
            	<td>
           			<textarea id="user.note" name="user.note" class="easyui-validatebox" rows="4" cols="25" maxlength="200"></textarea>
           		</td>
           	</tr>
        </table>
        </center>
        </form> 
	</div>
	<div id="dlg-buttons">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="savePro()">保存</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>  
</div> 
<div id="storeDlg" class="easyui-dialog" style="width:700px;height:450px;padding:10px 20px"  
        closed="true" buttons="#store-dlg-buttons"> 
        <div class="easyui-layout" fit="true" style="background:#ccc;">
			<div region="center" style="overflow:hidden;">
				<!-- 条件查询table -->
				<table style="font-size:12px; background:#fafafa; padding:10px;" border="0">
					<input type="hidden" id="wareClassId" name="wareClassId" value="" />
					<tr>
						<td> 门店名称
							<input type="text" id="wareName" name="wareName" class="easyui-validatebox"  value="${wareName}" />
						</td>
						<td>
							<a href="#" class="easyui-linkbutton" name="queryWare">查询</a>
						</td>
					</tr>
				</table>
			
				<table id="stores"></table>
			</div> 
		</div>
		<div id="store-dlg-buttons">  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="selectStore()">确认</a>  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#storeDlg').dialog('close')">取消</a>  
		</div> 
</div>
</body>
</html>
